package com.ciis.base.system;

import com.ciis.base.annotation.Auth;
import com.ciis.base.utils.SessionUtils;
import com.ciis.system.user.domain.TSysUser;
import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-5-28
 * Time: 下午9:37
 * To change this template use File | Settings | File Templates.
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {
    private final static Logger log= Logger.getLogger(AuthInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod method = (HandlerMethod)handler;
        // 使用Auth注解校验方法是否需要通过登录才可访问
        Auth auth = method.getMethod().getAnnotation(Auth.class);
        // 验证登陆超时问题  auth = null，默认验证
        if( auth == null || auth.verifyLogin()){
            String baseUri = request.getContextPath();
            String path = request.getServletPath();
            TSysUser user = SessionUtils.getUser(request);
            if(user  == null){
                /*if(path.indexOf("/login") != -1){*/
                    response.setStatus(response.SC_GATEWAY_TIMEOUT);
                    response.sendRedirect(baseUri+"/");
                    return false;
                /*}else{
                    response.setStatus(response.SC_GATEWAY_TIMEOUT);
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put(Constants.RESPONSE_SUCCESS, false);
                    result.put(Constants.RESPONSE_LOGOUT_FLAG, true);//登录标记 true 退出
                    result.put(Constants.RESPONSE_MSG, "登录超时.");
                    String json = JSONUtils.valueToString(result);
                    response.setHeader("Pragma", "No-cache");
                    response.setHeader("Cache-Control", "no-cache");
                    response.setCharacterEncoding("UTF-8");
                    PrintWriter out= null;
                    out = response.getWriter();
                    out.print(json);
                    out.flush();
                    out.close();
                    return false;
                }*/
            }
        }
        /*//验证URL权限
        if( auth == null || auth.verifyURL()){
            //判断是否超级管理员
            if(!SessionUtils.isAdmin(request)){
                String menuUrl = StringUtils.remove(request.getRequestURI(), request.getContextPath());;
                if(!SessionUtils.isAccessUrl(request, StringUtils.trim(menuUrl))){
                    //日志记录
                    // URL操作权限
                    return false;
                }
            }
        }*/
        // 向上下文中注入request、response
        ControllerContext.getContext().setRequest(request);
        ControllerContext.getContext().setResponse(response);
        return super.preHandle(request, response, handler);
    }
}
